package com.ychs.servlet;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationConfig;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.log4j.Logger;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * copyright(c)2021 YYB.ALL rights Reserved
 * <p>
 * 描述:
 *
 * @author 原玉波
 * @version 1.0
 * @date 2021/7/14
 */
@WebServlet(value = "/BaseServlet")
public class BaseServlet extends HttpServlet {
    protected Logger logger = Logger.getLogger(this.getClass());
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        logger.info("BaseServlet doPost");
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        String method = request.getParameter("method");
        switch (method) {
            case "add":
                add(request,response);
                break;
            case "toModify":
                toModify(request, response);
            case "modify":
                modify(request,response);
                break;
            case "remove":
                remove(request,response);
                break;
            case "search":
                search(request,response);
                break;
            case "searchById":
                searchById(request,response);
                break;
            case "verify":
                verify(request,response);
                break;
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }


    protected void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    }
    protected void toModify(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    }
    protected void modify(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    }
    protected void remove(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    }
    protected void search(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    }
    protected void searchById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    }
    protected void verify(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    }

    /**
     * 获取json字符串
     * @param count 总条数
     * @param data 总集合
     * @return json字符串
     * @throws JsonProcessingException
     */

    /*List<? extends Object> data 任何类型都可以，只要是Object的子类
    *   List<Object> data 只能是Object*/
    protected String json(int count,List<? extends Object> data) throws JsonProcessingException {
        //准备Json
        Map<String, Object> mapJSON = new HashMap<>();
        mapJSON.put("code", 0);
        mapJSON.put("msg", "");
        mapJSON.put("count", count);
        mapJSON.put("data", data);
        ObjectMapper mapper = new ObjectMapper();
        SimpleDateFormat myDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        mapper.setDateFormat(myDateFormat);
        //把Java对象转换为JSON字符串
        String json = mapper.writeValueAsString(mapJSON);
        return json;
    }


    /**
     * 获取json字符串
     * @param data 总集合
     * @return json字符串
     * @throws JsonProcessingException
     */

    /*List<? extends Object> data 任何类型都可以，只要是Object的子类
     *   List<Object> data 只能是Object*/
    protected String json(Object data) throws JsonProcessingException {
        //准备Json
        ObjectMapper mapper = new ObjectMapper();
        SimpleDateFormat myDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        mapper.setDateFormat(myDateFormat);
        //把Java对象转换为JSON字符串
        String json = mapper.writeValueAsString(data);
        return json;
    }

}
